<?php

namespace library;

/**
 * @Openssl加密解密类
 */
class OpensslEncrypt{
    /**
     * @var string 秘钥
     */
    public  $secret_key='jq_web_url';

    /**
     * @desc 加密
     * @param $str
     * @return array
     * @author zhaozhiwei
     * @time 2023/12/18-10:21
     */
    public function encrypt($str,$t_id){
        $encrypted = openssl_encrypt($str, 'AES-256-CBC', $this->secret_key, 0, $this->getRandom($t_id)); // 加密
        return $encrypted;
    }

    /**
     * @desc 解密
     * @param $str
     * @param $random
     * @return false|string
     * @author zhaozhiwei
     * @time 2023/12/18-10:21
     */
    public function decrypt($str,$t_id){
        $decrypted = openssl_decrypt($str, 'AES-256-CBC', $this->secret_key, 0, $this->getRandom($t_id)); // 解密
        return $decrypted;
    }

    /**
     * @desc 获取随机数方法
     * @return false|string
     * @author zhaozhiwei
     * @time 2023/12/18-10:21
     */
    protected function getRandom($template_id){
        return (new WaveCache())->waveGet('template:'.$template_id,function(){
            return openssl_random_pseudo_bytes(16);
        });

    }
}